﻿using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace DimensionsHelper.ExcelAddIn.Core;

public abstract class ExcelAddInSettingsBase : IExcelAddInSettings
{
    public abstract string Name { get; set; }

    public abstract bool Join(object transmission);

    public abstract void Recive(object transimission);

    public abstract IExcelAddInTransmission Transmit(string? name);

    protected T? ReadTransmission<T>(object transmission, JsonTypeInfo<T> jsonTypeInfo) where T : class
    {
        // 如果是未正常解析的Json对象，尝试重新解析
        if (transmission is JsonElement jsonObj && jsonObj.ValueKind == JsonValueKind.Object)
        {
            return jsonObj.Deserialize(jsonTypeInfo);
        }
        else if (transmission is T tran)
        {
            return tran;
        }

        return null;
    }
}
